package com.gdut.simple.model.impl;

import com.gdut.simple.common.LogUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.data.entity.Comment;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.model.ICommentModel;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class CommentModelImpl implements ICommentModel {

    @Override
    public void refresh(User user, final RequestCallback<List<Comment>> callback) {
        loadMore(user, 1, callback);
    }

    @Override
    public void loadMore(User user, int page, final RequestCallback<List<Comment>> callback) {
        LogUtils.i("CommentModelImpl loadMore user objectId is " + user.getObjectId());
        BmobQuery<Comment> query = new BmobQuery<>();
        query.addWhereEqualTo("user", user);
        query.include("user,post,post.author");
        query.setSkip(10 * (page - 1));
        query.findObjects(new FindListener<Comment>() {
            @Override
            public void done(List<Comment> list, BmobException e) {
                if (e == null) {
                    if (callback != null) {
                        callback.onSuccess(list);
                    }
                } else {
                    if (callback != null) {
                        callback.onFailure(e.getMessage());
                    }
                }
            }
        });
    }
}
